package com.bdqn.t320.ch05.controller;


import com.bdqn.t320.ch05.entity.SysUser;
import com.bdqn.t320.ch05.service.ISysUserService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author czkt
 * @since 2022-09-06
 */
@Controller
@RequestMapping("/ch05/sys-user")
public class SysUserController {


    @Resource
    ISysUserService iSysUserService;

    @ResponseBody
    @RequestMapping("/list")
    public Object list() {
        List<SysUser> list = iSysUserService.list(null);
        return list;
    }


    @RequestMapping("/login")
    public Object login() {
        return "login";
    }

    //@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @PostMapping("/doLogin")
    public Object doLogin(String usrName,
                          String usrPassword, @RequestParam(required = false) LocalDateTime birthday,
                          HttpSession session) {
        SysUser sysUser = iSysUserService.doLogin(usrName, usrPassword);
        if (sysUser == null) {
            return "redirect:error.jsp";
        } else {
            session.setAttribute("sysUser", sysUser);
            return "redirect:main";
        }
    }

    @RequestMapping("/error")
    public Object error() {
        return "error";
    }

    @RequestMapping("/main")
    public Object main(HttpSession session) {
        /*if (session.getAttribute("sysUser") == null) {
            return "redirect:error.jsp";
        }*/
        return "main";
    }


}
